package cn.miao.hrm.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.miao.client.LoginFeginClient;
import cn.miao.constant.BaseConst;
import cn.miao.hrm.domain.Employee;
import cn.miao.hrm.domain.Tenant;
import cn.miao.hrm.dto.LoginAgencyDto;
import cn.miao.hrm.dto.TenantRegisteredDto;
import cn.miao.hrm.mapper.EmployeeMapper;
import cn.miao.hrm.mapper.TenantMapper;
import cn.miao.hrm.service.ITenantService;
import cn.miao.util.AjaxResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author MiaoDaWei
 * @since 2021-11-20
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LoginFeginClient loginFeginClient;
    @Autowired
    private TenantMapper tenantMapper;

    /**
     * 机构入驻
     *
     * @param dto dto
     * @return {@link AjaxResult}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult entering(TenantRegisteredDto dto) {
        //参数校验---省略,可以用JSR303来检验并且使用全局异常来统一返回!
        checkParams(dto);

        //先保存保存t_tenant表t_employee表
        Date registerTime = new Date();

        Tenant tenant = dto.getTenant();
        tenant.setRegisterTime(registerTime);
        tenant.setState(BaseConst.TenantState.STATUS_AUDIT_WAIT);
        this.insert(tenant);

        Employee employee = dto.getEmployee();
        employee.setInputTime(registerTime);
        employee.setState(BaseConst.EmployeeState.EMPLOYEE_STATE_VALID);
        employee.setTenantId(tenant.getId());
        employee.setType(BaseConst.EmployeeState.EMPLOYEE_TYPE_TENANT_ADMIN);
        employeeMapper.insert(employee);

        //调用认证服务处理
        LoginAgencyDto agencyDto = new LoginAgencyDto();
        agencyDto.setLogin(dto.getLogin());
        agencyDto.setMealId(dto.getMealId());
        AjaxResult result = loginFeginClient.saveLoginAndRole(agencyDto);
        if (Objects.isNull(result) || !result.isSuccess()) {
            return AjaxResult.me().setMessage("机构认证失败").setSuccess(false);
        }

        employee.setLoginId(Convert.toLong(result.getResultObj()));
        employeeMapper.updateById(employee);
        tenant.setAdminId(employee.getId());
        this.updateById(tenant);

        return AjaxResult.me();
    }

    /**
     * 检查参数
     */
    public void checkParams(TenantRegisteredDto dto) {
        //公司名字不能重复
        EntityWrapper<Tenant> entityWrapper = new EntityWrapper();
        entityWrapper.eq("company_name", dto.getTenant().getCompanyName()).or();
        entityWrapper.eq("company_num", dto.getTenant().getCompanyNum());
        Integer selectCount = tenantMapper.selectCount(entityWrapper);
        Assert.isFalse(selectCount > 0, "公司名字或公司执照不能相同,请重新更换!");
    }
}
